package au.com.handsunited.web.controller.user;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import au.com.handsunited.model.User;
import au.com.handsunited.model.propertyeditors.JodaDateTimeEditor;
import au.com.handsunited.service.LanguageService;
import au.com.handsunited.service.ServiceCategoryService;
import au.com.handsunited.service.SkillService;
import au.com.handsunited.service.UserService;
import au.com.handsunited.utils.constants.DateConstants;
import au.com.handsunited.utils.constants.RegionConstants;
import au.com.handsunited.web.command.user.UserCommand;
import au.com.handsunited.web.session.SessionSupport;

/**
 *
 * @author Arun Chandrasekhar
 */
public class UserEditProfileController extends SimpleFormController {

	private UserService userService;
	private LanguageService languageService;
	private SkillService skillService;
	private DateConstants dateConstants;
	private RegionConstants regionConstants;
	private ServiceCategoryService serviceCategoryService;

	private static Logger logger = LoggerFactory.getLogger(UserEditProfileController.class);


	/**
	 *
	 */
	public UserEditProfileController() {
		setCommandClass(UserCommand.class);
	}


	/**
	 * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.validation.BindException)
	 */
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {

		UserCommand userCommand = (UserCommand) command;
		User user = userCommand.getUser();
		user = userService.updateUser(user);

		SessionSupport.setLoggedInUser(request, user);

		Map<String, String> model = new HashMap<String, String>();
		model.put("userId", user.getId().toString());
		model.put("userName", user.getName());

		ModelAndView mav = new ModelAndView(getSuccessView(), model);
		return mav;
	}


	/**
	 * @see org.springframework.web.servlet.mvc.SimpleFormController#referenceData(javax.servlet.http.HttpServletRequest, java.lang.Object, org.springframework.validation.Errors)
	 */
	@Override
	protected Map referenceData(HttpServletRequest request, Object command,
			Errors errors) throws Exception {

		Map<String, Object> data = new HashMap<String, Object>();
		data.put("countries", regionConstants.getCountries());
		data.put("states", regionConstants.getStates());
		data.put("days", dateConstants.getDays());
		data.put("months", dateConstants.getMonths());
		data.put("years", dateConstants.getPastYears());
		data.put("hours", dateConstants.getHours());
		data.put("minutes", dateConstants.getMinutes());
		data.put("languages", languageService.getAllLanguages());
		data.put("skills", skillService.getAllSkills());
        data.put("serviceCategories", serviceCategoryService.getAllServiceCategories());

		return data;
	}


	/**
	 * @see org.springframework.web.servlet.mvc.BaseCommandController#initBinder(javax.servlet.http.HttpServletRequest, org.springframework.web.bind.ServletRequestDataBinder)
	 */
	@Override
	protected void initBinder(HttpServletRequest request,
			ServletRequestDataBinder binder) throws Exception {

		super.initBinder(request, binder);
		binder.registerCustomEditor(DateTime.class, new JodaDateTimeEditor());
	}


	/**
	 * @see org.springframework.web.servlet.mvc.AbstractFormController#formBackingObject(javax.servlet.http.HttpServletRequest)
	 */
	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {

		User user = SessionSupport.getUser(request);
		UserCommand command = new UserCommand(user);

		command.setLanguageMap(languageService.getLanguagesMap());
		command.setSkillMap(skillService.getSkillsMap());
		command.setServiceCategoryMap(serviceCategoryService.getServiceCategoryMap());

		return command;
	}

	/**
	 *
	 * @param userService
	 */
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	/**
	 * @param dateConstants the dateConstants to set
	 */
	public void setDateConstants(DateConstants dateConstants) {
		this.dateConstants = dateConstants;
	}

	/**
	 * @param regionConstants the regionConstants to set
	 */
	public void setRegionConstants(RegionConstants regionConstants) {
		this.regionConstants = regionConstants;
	}


	/**
	 * @param languageService the languageService to set
	 */
	public void setLanguageService(LanguageService languageService) {
		this.languageService = languageService;
	}


	/**
	 * @param skillService the skillService to set
	 */
	public void setSkillService(SkillService skillService) {
		this.skillService = skillService;
	}


	/**
	 * @param serviceCategoryService the serviceCategoryService to set
	 */
	public void setServiceCategoryService(ServiceCategoryService serviceCategoryService) {
		this.serviceCategoryService = serviceCategoryService;
	}

}
